package games;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;


public class PrisonersDilemma extends Game {
	/**
	 * 
	 */
	private static final long serialVersionUID=1L;

	private static final String GAME_NAME="PrisonersDilemma";
	private static final String DATA_FOLDER="games\\data\\";
	private static final String PLAYER_NAME="Player";
	private static final String SUFFIX=".data";
	private static final String COOPERATE="Cooperate";
	private static final String DEFECT="Defect";
	private static final String CC="CC";
	private static final String CD="CD";
	private static final String DD="DD";
	
	int cc;
	int c;
	int d;
	int dd;
	Vector<Move> otherPlayer=new Vector<Move>();

	public PrisonersDilemma(Runner runner,int cc,int c,int d,int dd) {
		super(runner,GAME_NAME);
		this.cc=cc;
		this.c=c;
		this.d=d;
		this.dd=dd;
	}
	
	public PrisonersDilemma(Runner runner) {
		this(runner,3,0,5,1);
	}
	
	public void init() {
		// Turn off metal's use of bold fonts
		UIManager.put("swing.boldMetal",Boolean.FALSE);
	}

	public void start() {
		initComponents();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame f=new JFrame("Prisoners Dilemma");
		f.setLayout(new BorderLayout());
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Toolkit toolkit = Toolkit.getDefaultToolkit();
    //Dimension screenSize = toolkit.getScreenSize();
		PrisonersDilemma ap=new PrisonersDilemma(new Runner());
		ap.init();
		ap.start();
		f.add("Center",ap);
		f.pack();
		f.setVisible(true);
		f.setSize(600,500);
	}
	
	public void getOtherPlayer() {
		try {
			//PrisonersDilemmaPlayerCC33CD05DD11.data
			File f=new File(DATA_FOLDER+GAME_NAME+PLAYER_NAME+CC+cc+cc+CD+c+d+DD+dd+dd+SUFFIX);
			//System.out.println(f.exists());			
			BufferedReader r=new BufferedReader(new FileReader(f));
			//System.out.println(r.readLine());
			
			String[] tokens;
			String current;
			
			while((current=r.readLine())!=null) {
				tokens=current.split("\t");
				otherPlayer.add(new Move(tokens[0],tokens[1].equals(COOPERATE)));
			}
			//for(Vector<Move>v:otherPlayer) for(Move m:v) System.out.println(m);
			//System.out.println(otherPlayer);
			
			
		} catch(FileNotFoundException e) {
			System.err.println("Can't find the file.");
			e.printStackTrace();
		} catch(IOException e) {
			System.err.println("Problem reading the file.");
			e.printStackTrace();
		}
	}

	public boolean getMove() {
		Random random=new Random();
		Move move=otherPlayer.get(random.nextInt(otherPlayer.size()));
		log(move.toString());
		
		System.out.println(otherPlayer);
		
		return move.cooperate;
	}
	
	class Move {
		String id;
		boolean cooperate;
		
		public Move(String id,boolean cooperate) {
			this.id=id;
			this.cooperate=cooperate;
		}
		
		public String toString() {
			return id+"\t"+(cooperate?COOPERATE:DEFECT);
		}
	}
	
	public void initComponents() {
		setLayout(new BorderLayout());
		getOtherPlayer();
		
		go=new Painter("go","Click to Cooperate");
		stop=new Painter("stop","Click to Defect");
		
		main=new JPanel(new BorderLayout());
		main.add(BorderLayout.EAST,go);
		main.add(BorderLayout.WEST,stop);
		
		add(BorderLayout.SOUTH,main);
		
		clock=new Timing();
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"),"selector");
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("G"),"changer");
		
		getActionMap().put("selector",new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID=1L;

			public void actionPerformed(ActionEvent e) {
				//System.err.println("Selector pushed");
				suspend();
				time=clock.getElapsedTime();
				log(DEFECT,getMove()?c:dd);
				finished(GAME_NAME,time);
			}
		});		
		getActionMap().put("changer",new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID=1L;

			public void actionPerformed(ActionEvent e) {
				//System.err.println("Changer pushed");
	      	suspend();
	      	time=clock.getElapsedTime();
					log(COOPERATE,getMove()?cc:d);
					finished(GAME_NAME,time);
			}
		});
		
    proceed();
		
		
		
		/*//hmmm(NAME);
		clock=new Timing();
    addMouseListener(new MouseAdapter(){
      public void mousePressed(MouseEvent e){
        //get the mouse position
        int xPos=e.getX();
        int yPos=e.getY();
        System.err.println("Mouse pressed at "+xPos+", "+yPos);
        log(e.getX()<300?HOLD_OUT:COOPERATE,e.getX()<300?1.:5.);
        finished(NAME,time);
      }
    });*/
	}
}